package com.lyb.controller;

import com.lyb.Dto.NameRoleDto;
import com.lyb.Dto.UserDto;
import com.lyb.common.AjaxResult;
import com.lyb.pojo.Role;
import com.lyb.pojo.User;
import com.lyb.services.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

@Api(tags="User")
@RestController
@RequestMapping("/user")
public class UserC {

    @Autowired
    UserServiceImpl userService;
    @ApiOperation("注册")
    @PostMapping("/register")
    public Boolean register(@RequestBody UserDto user){
        String Password = user.getPassword();
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        user.setPassword(passwordEncoder.encode(Password));
        return  userService.register(user);
    }
    @GetMapping("/queryPage")
    public AjaxResult queryPage(Integer pageSize, Integer pageNum){
        return AjaxResult.success(userService.queryPage(pageSize,pageNum));
    }
    @PostMapping("/update")
    public AjaxResult update(@RequestBody User user){
        userService.update(user);
        return AjaxResult.success("操作成功");
    }
    @DeleteMapping("/del")
    public AjaxResult del(Long id){
        userService.del(id);
        return AjaxResult.success("操作成功");
    }
    @PostMapping("/add")
    public AjaxResult add(@RequestBody NameRoleDto nameRoleDto){
        User user = new User();
        user.setName(nameRoleDto.getName());
//        System.out.print(name);
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        user.setPassword(passwordEncoder.encode("123456"));
        userService.add(user,nameRoleDto.getRoleId());
        return AjaxResult.success("操作成功");
    }
    @GetMapping("/findByUserId")
    public AjaxResult findByUserId(Long id){
        return AjaxResult.success(userService.findByUserId(id));
    }
}
